package order

import (
	"context"
	"errors"
	"fmt"
	"group-buy/group_buy/internal/common"
	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/request_models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GenerateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateLogic {
	return &GenerateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GenerateLogic) Generate(req *types.GenerateReq) (resp *types.GenerateRsp, err error) {
	l.Logger.Infof("%#v\n", req)
	user := &models.Member{Id: req.UserId}
	get, err := l.svcCtx.Engine.Get(user)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("会员获取失败")
	}
	product := &models.Product{Id: req.ProductId}
	get, err = l.svcCtx.Engine.Get(product)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("产品获取失败")
	}

	businessId := product.BusinessId
	if req.BusinessId > 0 {
		businessId = req.BusinessId
	}
	business := &models.Business{Id: businessId}
	get, err = l.svcCtx.Engine.Get(business)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("获取商家失败")
	}

	order := &models.Order{
		Id:            0,
		Code:          common.GetStrId(),
		UserId:        user.Id,
		ProductId:     product.Id,
		Amount:        product.Price,
		UserName:      user.Name,
		ProductName:   product.Name,
		WriteOffCount: product.WriteOff,
		FinishCount:   0,
		Status:        1,
		PayOrderId:    "",
		OpenId:        user.OpenId,
		BusinessId:    business.Id,
		DealerId:      business.DealerId,
		InviteId:      req.InviteId,
		ProductCover:  product.Cover,
	}
	division, err := order.GetOrderDivision(l.svcCtx.Engine)
	if err != nil {
		return nil, err
	}

	Extra := new(request_models.MiniAppPayExtra)
	divModel := int64(0)
	if len(division) > 0 {
		Extra = &request_models.MiniAppPayExtra{DivList: division}
		divModel = 1
	}

	lsPay := &request_models.LsMiniAppPay{
		MchNo:        l.svcCtx.Config.LsPay.MchNo,
		AppId:        l.svcCtx.Config.LsPay.AppId,
		MchOrderNo:   order.Code,
		WayCode:      "WX_LITE_JUMP",
		Amount:       int64(product.Price * 100),
		Currency:     "cny",
		Subject:      product.Name,
		Body:         product.Name,
		NotifyUrl:    "http://cqjmc.com/group_buy/miniapp/order/order_notify",
		DivisionMode: divModel,
		ChannelExtra: Extra,
		ReqTime:      common.GetIntId(),
		Version:      "1.0",
		SignType:     "MD5",
	}
	pay, err := lsPay.Pay(l.svcCtx.Config.LsPay.PayKey)
	if err != nil {
		return nil, err
	}
	fmt.Printf("qqqq%#v", pay)
	if pay.Status == 200 && len(pay.Data.PayOrderId) > 0 {
		order.Code = pay.Data.MchOrderNo
		order.PayOrderId = pay.Data.PayOrderId
		_, err = l.svcCtx.Engine.Insert(order)
		if err != nil {
			return nil, err
		}

		// return &types.GenerateRsp{PayInfo: &types.WxPayInfo{
		// 	TimeStamp: pay.Data.PayInfo.TimeStamp,
		// 	Package:   pay.Data.PayInfo.Package,
		// 	PaySign:   pay.Data.PayInfo.PaySign,
		// 	AppId:     pay.Data.PayInfo.AppId,
		// 	SignType:  pay.Data.PayInfo.SignType,
		// 	NonceStr:  pay.Data.PayInfo.NonceStr,
		// }}, nil

		return &types.GenerateRsp{PayInfo: &types.WxPayInfo{AppId: pay.Data.PayInfo.AppId, Path: pay.Data.PayInfo.Path, SchemeCode: pay.Data.PayInfo.SchemeCode, PayOrderId: pay.Data.PayOrderId}}, nil

	}
	return nil, errors.New(pay.Msg)
}
